<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>商品购物车</title>
    <link rel="shortcut icon" href="./../../favicon.ico">
    <link rel="stylesheet" href="../../style/cart/shoppingCart.css">
    <script type="text/javascript" src="./../../plugins/jQuery/jquery-3.6.0.js"

    ></script>
    <script type="text/javascript" src="./../../plugins/vue/vue.js"

    ></script>
    <script type="text/javascript" src="./../../plugins/axios/axios.js"

    ></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="./../../plugins/element-ui/index.css">
    <!-- 引入组件库 -->
    <script src="./../../plugins/element-ui/index.js"

    ></script>
    <!-- 引入layer -->
    <script src="../../plugins/layer-master/dist/layer.js"

    ></script>
    <style>
        .el-table {
            margin: 0 auto;
        }
    </style>
</head>
<body>
<div id="app">
    <nav class="public-header">
        <a href="../../index.html">网站首页</a>
        <a href="">我的订单</a>
    </nav>
    <div class="cart">
        <template>
            <el-table
                    ref="multipleTable"
                    :data="cartItems"
                    tooltip-effect="dark"
                    style="width: 87%"
            >
                <el-table-column
                        type="selection"
                        width="55"
                        align="center"
                >
                </el-table-column>
                <el-table-column
                        label="商品"
                        width="220"
                        align="center"
                >
                    <template slot-scope="scope">
                        <img width="120px" :src=`http:${scope.row.imageName}`>
                    </template>
                </el-table-column>
                <el-table-column
                        label="书名"
                        align="center"
                >
                    <template slot-scope="scope">
                        {{scope.row.name}}
                    </template>
                </el-table-column>
                <el-table-column
                        label="出版社"
                        align="center"
                >
                    <template slot-scope="scope">
                        {{scope.row.publisher}}
                    </template>
                </el-table-column>
                <el-table-column
                        prop="price"
                        label="单价"
                        width="120"
                        align="center"
                >
                    <template slot-scope="scope">￥{{ scope.row.price }}元</template>
                </el-table-column>
                <el-table-column
                        prop="number"
                        label="数量"
                        show-overflow-tooltip
                        align="center"
                        width="200px"
                >
                    <template slot-scope="scope">
                        <el-button size="mini" type="danger" @click="updateCart(scope.row,-1)">-1</el-button>
                        <input type="text"
                               style="width: 50px; height: 23px"
                               v-model.number="scope.row.number">
                        <el-button size="mini" type="success" @click="updateCart(scope.row,1)">+1</el-button>
                    </template>

                </el-table-column>
                <el-table-column
                        prop="address"
                        label="小计"
                        show-overflow-tooltip
                        align="center"
                        width="120"
                >
                    <template slot-scope="scope">

                        <span>
                            ￥{{ (scope.row.price * scope.row.number).toFixed(2) }}元
                        </span>


                    </template>
                </el-table-column>
                <el-table-column
                        prop="address"
                        label="操作"
                        show-overflow-tooltip
                        align="center"
                        width="120"
                >
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                type="danger"
                                align="center"
                                @click="removeGwc(scope.row.book_id)"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-button type="danger" class="dangers" v-if="cartItems.length > 0" id="batchDeleteBtn"
            >
                批量删除
            </el-button>
            <div class="cart-btn"><span>总价：</span><span>  </span>
                <button class="btn">去结算</button>
            </div>
        </template>
        <!-- 页脚部分 -->
        <div class="extra">
            <div class="wrapper">
                <!-- 版权信息 -->
                <div class="copyright">
                    <p>
                        <a href="javascript:;">关于我们</a>
                        <a href="javascript:;">帮助中心</a>
                        <a href="javascript:;">售后服务</a>
                        <a href="javascript:;">配送与验收</a>
                        <a href="javascript:;">商务合作</a>
                        <a href="javascript:;">搜索推荐</a>
                        <a href="javascript:;">友情链接</a>
                    </p>
                    <p>CopyRight &copy; 加中实训</p>
                </div>
            </div>
        </div>
    </div>
</div>


</body>
</html>
<script>
    new Vue({
        el: '#app',
        data: {
            cartItems: []
        },
        methods: {
            loadMyCart() {
                let user_id = localStorage.getItem('user_id');
                if (user_id === undefined || user_id === null) {
                    location.href = '../login/login.html'
                    return;
                }
                axios({
                    method: 'get',
                    url: 'http://localhost:8888/shopping_cart/find',
                    params: {
                        user_id: user_id
                    }
                }).then(resp => {
                    this.cartItems = resp.data.data
                })
            },
            removeGwc(book_id) {
                let user_id = localStorage.getItem('user_id');
                axios({
                    method: 'get',
                    url: 'http://localhost:8888/shopping_cart/remove1',
                    params: {
                        user_id: user_id,
                        book_id: book_id
                    }
                }).then(resp => {
                    // 刷新页面
                    this.loadMyCart()
                })
            },
            updateCart(row, n) {
                let user_id = localStorage.getItem('user_id');
                row.number = row.number - 0 + n
                axios({
                    method: 'get',
                    url: 'http://localhost:8888/shopping_cart/update',
                    params: {
                        user_id: user_id,
                        book_id: row.book_id,
                        number: n-0
                    }
                }).then(resp => {
                    // 刷新页面
                    this.loadMyCart()
                })
            }
        },
        created() {
            this.loadMyCart()
        }
    })
</script>